package com.hh.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.hh.springboot.model.Commodity;
import com.hh.springboot.model.OrderForm;
import com.hh.springboot.model.User;
import com.hh.springboot.service.CommodityService;
import com.hh.springboot.service.OrderService;
import com.hh.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据控制器
 */
@RestController
public class DataController {
//    @Autowired
//    private DataSource dataSource;

    @Autowired
    private CommodityService commodityService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    private OrderForm orderForm;

    @RequestMapping("/login")
    public String login(User user, HttpSession session) {
        User login = userService.login(user);
        session.setAttribute("user", login);
        Map<String, String> map = new HashMap<>();
        if (login != null) {
            map.put("message", "成功");
        } else {
            map.put("message", "失败");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/getUser")
    public String selLoginUser(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Map<String, User> map = new HashMap<>();
        map.put("user", user);
        return JSON.toJSONString(map);
    }

    @RequestMapping("/foods")
    public String getFoods() {
        List<Commodity> commodities = commodityService.getCommodities();
        Map<String, Object> map = new HashMap<>();
        map.put("foods", commodities);
        return JSON.toJSONString(map);
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        Map<String, String> map = new HashMap<>();
        map.put("message", "/foodCity");
        return JSON.toJSONString(map);
    }
}
